/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

import org.springframework.dao.DataAccessException ;
import org.springframework.dao.DataIntegrityViolationException ;

import org.jiscinvolve.astrodabis.barberry.common.core.exception.*;

/*
 *
 */
public class AccountTagsTestCase
extends CoreTestBase
    {

    @Test
    public void checkCreatedNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertNotNull(
            created
            );
        }    

    @Test
    public void checkCreatedIdentNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertNotNull(
            created.ident()
            );
        }    

    @Test
    public void checkCreatedNameNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertNotNull(
            created.name()
            );
        }    

    @Test
    public void checkCreatedNameEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        assertEquals(
            "test-tag-name",
            created.name()
            );
        }    

    @Test
    public void checkCreatedTextNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertNotNull(
            created.text()
            );
        }    

    @Test
    public void checkCreatedOwnerNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertNotNull(
            created.owner()
            );
        }    

    @Test
    public void checkCreatedOwnerEquals()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertEquals(
            account(),
            created.owner()
            );
        }    

    @Test
    public void checkCreatedOwnerIdentEquals()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        assertEquals(
            account().ident(),
            created.owner().ident()
            );
        }    


    @Test
    public void checkSelectByNameEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created,
            selected
            );
        }    

    @Test
    public void checkSelectByNameIdentEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created.ident(),
            selected.ident()
            );
        }    

    @Test
    public void checkSelectByNameNameEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created.name(),
            selected.name()
            );
        }    

    @Test
    public void checkSelectByNameTextEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created.text(),
            selected.text()
            );
        }    

    @Test
    public void checkSelectByNameOwnerEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created.owner(),
            selected.owner()
            );
        }    

    @Test
    public void checkSelectByNameOwnerIdentEquals()
        {
        Tag created = account().tags().create(
            "test-tag-name"
            );
        Tag selected = account().tags().select(
            "test-tag-name"
            );
        assertEquals(
            created.owner().ident(),
            selected.owner().ident()
            );
        }    

    @Test
    public void checkSelectByIdentNotNull()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        Tag selected = womble().tags().select(
            created.ident()
            );
        assertNotNull(
            selected
            );
        }    

    @Test
    public void checkSelectByIdentEquals()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        Tag selected = womble().tags().select(
            created.ident()
            );
        assertEquals(
            created,
            selected
            );
        }    

    @Test
    public void checkSelectByIdentIdentEquals()
        {
        Tag created = account().tags().create(
            unique(
                "test-tag"
                )
            );
        Tag selected = womble().tags().select(
            created.ident()
            );
        assertEquals(
            created.ident(),
            selected.ident()
            );
        }    

    @Test
    public void checkDupNameReturnsSame()
        {
        String name = unique(
            "test-tag"
            );
        compare(
            account().tags().create(
                name
                ),
            account().tags().create(
                name
                )
            );
        }


    @Test
    public void checkDupNameThrowsException()
        {
        try {
            account().tags().create(
                account().tags().create(
                    unique(
                        "test-tag"
                        )
                    ).name(),
                "test-tag-text"
                );
            fail("DuplicateEntryException expected");
            }
        catch(DuplicateEntryException ouch)
            {
            }
        }

    @Test
    public void checkNameNamespace()
        {
        Account frog = womble().accounts().create(
            unique(
                "test-account"
                ),
            "test-pass"
            );
        Account toad = womble().accounts().create(
            unique(
                "test-account"
                ),
            "test-pass"
            );
        String tagname = unique(
            "test-tag"
            );

        Tag frogstag = frog.tags().create(
            tagname
            );

        Tag toadstag = toad.tags().create(
            tagname
            );

        assertFalse(
            frogstag.equals(
                toadstag
                )
            );
        assertFalse(
            frogstag.ident().equals(
                toadstag.ident()
                )
            );
        assertTrue(
            frogstag.name().equals(
                toadstag.name()
                )
            );

        Tag frogsdup = frog.tags().create(
            tagname
            );

        Tag toadsdup = toad.tags().create(
            tagname
            );

        compare(
            frogstag,
            frogsdup
            );

        compare(
            toadstag,
            toadsdup
            );

        }
    }

